package com.flow.manager.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.struts2.ServletActionContext;

import com.flow.model.BackInfo;
import com.flow.model.PagerModel;
import com.flow.model.RoleTab;
import com.flow.service.IRoleTabService;
import com.flow.tools.StringTools;

/**
 * 角色控制器
 * 
 * @author wang
 * @2016-1-6 RoleTabAction.java
 * 
 */
public class RoleTabAction {

	BackInfo back = new BackInfo();

	/**
	 * 服务层引用
	 */
	private IRoleTabService<RoleTab> rtService;

	/**
	 * 添加角色菜单
	 */
	public String insertRoleTab() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("rtName");
		String status = request.getParameter("rtStatus");
		String remark = request.getParameter("remark");
		status = (!StringTools.nullOrEmpty(status)) ? "0" : status;

		try {

			RoleTab roleTab = new RoleTab(name, Integer.valueOf(Integer.parseInt(status)), remark);
			if (this.rtService.insertRoleTab(roleTab))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 更新角色信息 
	 */
	public String updateRoleTab() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("rtId");
		String name = request.getParameter("rtName");
		String status = request.getParameter("rtStatus");
		String remark = request.getParameter("remark");
		status = (!StringTools.nullOrEmpty(status)) ? "0" : status;
		
		try {
			
			RoleTab roleTab = new RoleTab(Integer.valueOf(id), name, Integer.valueOf(Integer.parseInt(status)), remark);
			if (this.rtService.updateRoleTab(roleTab))
				return "SUCCESS";
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 编辑角色 
	 */
	public String inputUpdate() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String rtId = request.getParameter("rtId");
		
		try {
			
			RoleTab roleTab = this.rtService.findRoleTabById(rtId);
			request.setAttribute("roleTab", roleTab);
			return "SUCCESS";
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 验证角色名
	 */
	public String validateRoleName() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		String name = request.getParameter("rtName");
		
		try {
			
			if (this.rtService.vlidateName(name))
				back.setBackCode("000");
			else {
				back.setBackCode("001");
			}
			request.setAttribute("back", back);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			return null;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 查询全部角色
	 */
	public String findAllRoleTab() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			
			List<RoleTab> rtList = this.rtService.findAllRoleTab();
			request.setAttribute("rtList", rtList);
			return null;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 分页 
	 */
	public String findAllRoleTabByPager() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		try {
			
			PagerModel pager = this.rtService.findAllRoleTabByPager();
			request.setAttribute("pager", pager);
			return null;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 删除角色
	 */
	public String deleteRoleTab() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		try {
			
			RoleTab rt = rtService.findRoleTabById(id);
			if (this.rtService.deleteRoleTab(rt))
				return "SUCCESS";
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	/**
	 * 根据ID获取角色
	 */
	public String findRoleTabById() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		try {
			
			RoleTab roleTab = this.rtService.findRoleTabById(id);
			request.setAttribute("roleTab", roleTab);
			return null;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return "ERROR";
	}

	public void setRtService(IRoleTabService<RoleTab> rtService) {
		this.rtService = rtService;
	}
}
